package ${package.ServiceImpl};


import ${package.Mapper}.${table.mapperName};
import ${package.Service}.*;
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;


import ${package.ReqVoPackage}.*;
import ${package.RespVoPackage}.*;
import ${package.Entity}.*;

import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.hutool.core.collection.CollectionUtil;
import com.yingxin.yx.framework.commons.helper.ListHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import java.util.ArrayList;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

<#if buildType == 'NORMAL'>

    public ${table.mapperName} mapper() {
        return baseMapper;
    }

    /**
    * 创建
    *
    * @param ${entityVariable}
    *
    */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create(${entity} ${entityVariable}) {
        save(${entityVariable});
    }


</#if>
<#if buildType == 'BILL'>
    @Autowired
    private ${itemServiceClass} ${itemServiceClassVariable};

    public ${table.mapperName} mapper() {
        return baseMapper;
    }

    /**
    * 创建
    *
    * @param ${billClassVariable}
    * @param items
    */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create(${billClass} ${billClassVariable}, List<${itemClass}> items) {
        save(${billClassVariable});
        if (CollectionUtil.isNotEmpty(items)) {
            ${itemServiceClassVariable}.saveBatch(items);
        }
    }


</#if>

<#if buildType == 'ITEM'>

    public ${table.mapperName} mapper() {
        return baseMapper;
    }

    /**
    * 根据 ${foreignProperty} 查询
    *
    * @param ${foreignProperty}
    * @return
    */
    @Override
    public List<${itemClass}> listBy${firstLetterUpperForeignProperty}(String ${foreignProperty}) {
        QueryWrapper<${itemClass}> qw = new QueryWrapper<>();
        qw.lambda().eq(${itemClass}::get${firstLetterUpperForeignProperty}, ${foreignProperty});
        return list(qw);
    }

    /**
    * 根据 ${foreignProperty} 删除明细
    *
    * @param ${foreignProperty}
    * @return
    */
    @Override
    public Boolean deleteBy${firstLetterUpperForeignProperty}(String ${foreignProperty}) {
        UpdateWrapper<${itemClass}> uw = new UpdateWrapper<>();
        uw.lambda().eq(${itemClass}::get${firstLetterUpperForeignProperty}, ${foreignProperty});
        return remove(uw);
    }

    /**
    * 根据 eq ${foreignProperty} , not in ids 删除明细
    *
    * @param ${foreignProperty}
    * @param ids
    * @return
    */
    @Override
    public Boolean deleteBy${firstLetterUpperForeignProperty}AndIdNotIn(String ${foreignProperty}, List<String> ids) {
        UpdateWrapper<${itemClass}> uw = new UpdateWrapper<>();
        uw.lambda().eq(${itemClass}::get${firstLetterUpperForeignProperty}, ${foreignProperty});
        if (CollectionUtil.isNotEmpty(ids)) {
            uw.lambda().notIn(${itemClass}::getId, ids);
        }
        return remove(uw);
    }

    /**
    *
    *  更新 items
    */
    public void updateBillItems(String ${foreignProperty}, List<${itemClass}> items) {
        List<String> itemIds = ListHelper.toList(${itemClass}::getId, items);
        deleteBy${firstLetterUpperForeignProperty}AndIdNotIn(${foreignProperty}, itemIds);
        if (CollectionUtil.isNotEmpty(items)) {
            saveOrUpdateBatch(items);
        }
    }

</#if>

}
</#if>
